<template>
	<view class="gl-popup-view">
		<view class="gl-popup-title">
			<view class=""> 上传物流运单 </view>
			<uni-icons class="gl-button-mini gl-button-error" type="closeempty" size="18" color="#fff"
				@tap="EventClose"></uni-icons>
		</view>
		<view class="gl-popup-body" style="display: flex;gap: 5px;">
			<view style="display: flex;flex-direction: column;gap: 5px; flex: 1;">
				<view style="display: flex;">
					<uni-data-select class="gl-data-select" v-model="past"
						:localdata="[{value:'virtl',text:'虚拟商品'},{value:'local',text:'线下自提'},{value:'email',text:'快递物流'}]"
						emptyTips="没有" label="发货方式" placeholder="选择发货方式" style="width: 250px;"
						@change="radioChange"></uni-data-select>
				</view>
				<view v-if="past == 'email'" style="display: flex;">
					<uni-data-select class="gl-data-select" v-model="curCarry.value" :localdata="deliList"
						emptyTips="没有" label="物流公司" placeholder="选择物流公司" style="width: 250px;"
						@change="changeCarry"></uni-data-select>
				</view>
				<view v-if="past == 'email'" class="" style="display: flex;align-items: center;">
					<view class="gl-label-text">物流单号：</view>
					<uni-easyinput v-model="curCarry.tracking_no" placeholder="请输入物流单号" />
				</view>
				<view v-if="past == 'email'" class="" style="display: flex;align-items: center;">
					<uni-data-select class="gl-data-select" v-model="curCarry.curPid" :localdata="pids" emptyTips="没有"
						label="平台商户" placeholder="选择平台商户" style="width: 250px;" @change="changePids"></uni-data-select>
				</view>
			</view>
			<scroll-view scroll-y="true" style="max-height: 30vh;">
				<checkbox-group @change="changeCheck">
					<view v-for="(item,index) in allProduct" :key="index"
						style="display: flex;gap: 5px;align-items: center;">
						<text style="font-weight: bold;">{{index + 1}}</text>
						<image :src="sConf.cos + (item.ext_product.cover[0].url || '/media/default/default.png')"
							mode="" style="width: 64px;height: 86px;border-radius: 5px;">
						</image>
						<view style="display: flex;flex-direction: column;gap: 5px;">
							<text>{{item.ext_product.idcode}}</text><text>{{item.ext_product.sdcode}}</text><text>{{item.ext_product.name}}</text>
						</view>
						<text style="font-weight: bold;padding: 0 10px;"> * </text>
						<uni-number-box :min="1" :max="item.num" :value="item.num" disabled=""
							@change="(e)=>{changeNum(item.ext_product.id, e)}" />
						<label>
							<checkbox :value="String(item.id)" :checked="filterCheck(item.id)" />
						</label>
					</view>
				</checkbox-group>
			</scroll-view>
		</view>
		<view class="gl-popup-foot" style="display: flex;justify-content: flex-end;gap: 10px;">
			<view class="gl-button gl-button-base" @tap="EventSubmit">
				<uni-icons type="checkmarkempty" size="18" color="#fff"></uni-icons>
				<text style="font-size: small;">保存</text>
			</view>
			<view class="gl-button gl-button-warning" @tap="EventClose">
				<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
				<text style="font-size: small;">取消</text>
			</view>
		</view>
	</view>
</template>

<script>
	import psMethod from '@/common/psMethod';
	export default {
		data() {
			return {
				sConf: getApp().globalData,
				past: 'virtl', // 发货方式
				deliList: [], // 物流公司列表		
				allProduct: [], // 当前的产品		
				curCarry: {
					value: '', // 物流公司代码
					tracking_no: '', // 物流单号
					curPid: null, // 当前商户
				},
				curCheck: [], // 勾选的产品
			}
		},
		mounted() {
			this.loadDeliveryList()
		},
		props: {
			reqsn: {
				type: String,
				default: null
			},
			order: {
				type: String,
				default: null
			},
			pids: {
				type: Array,
				default: []
			},
			prot: {
				type: Array,
				default: []
			}
		},
		methods: {
			radioChange(e) {
				console.log('radioChange', e);
				this.past = e
			},
			EventClose() {
				this.$emit('eventClose')
			},
			EventSubmit() {
				console.log('EventSubmit', this.curCarry);
				if (this.past === 'virtl' || this.past === 'local') {
					this.loadDeliveryLine()
					return
				}
				if (this.curCarry.value === '' || this.curCarry.tracking_no === '' || this.curCarry.curPid ===
					null) {
					uni.showToast({
						title: '单据字段缺失'
					})
					return
				}
				if (this.curCheck.length === 0) {
					uni.showToast({
						title: '产品缺失'
					})
					return
				}
				this.loadDeliveryPost()
			},
			filterData(e) {
				return this.deliList.find(item => item.value === e)
			},
			filterCheck(e) {
				return this.curCheck.includes(String(e))
			},
			async loadDeliveryLine() {
				console.log('线下提货');
				let host = this.sConf.host + '/admin/offLine/';
				let token = this.sConf.auth.token || '';
				let data = {
					order: this.order,
					past: this.past
				}
				await psMethod.authenticatorPostSync(host, token, data).then(res => {
					console.log('提交物流单', res);
					uni.showToast({
						title: res.resmsg
					})
					this.$emit('eventAccept')
				}).catch(err => {
					console.log('失败', err);
					uni.showToast({
						title: '失败'
					})
				})
			},
			async loadDeliveryList() {
				let host = this.sConf.host + '/api/carry/';
				let token = this.sConf.auth.token || '';
				let res = await psMethod.authenticatorPostSync(host, token)
				console.log('加载快递公司', res);
				if (res.rescode === 200) {
					let tmpData = []
					res.resmsg.forEach(item => {
						tmpData.push({
							value: item.delivery_id,
							text: item.delivery_name
						})
					})
					this.deliList = tmpData;
				}
			},
			async loadDeliveryPost() {
				let host = this.sConf.host + '/admin/express/';
				let token = this.sConf.auth.token || '';
				let data = {
					order: this.order,
					name: this.filterData(this.curCarry.value).text,
					express: this.curCarry.value,
					tracking: this.curCarry.tracking_no,
					pid: this.curCarry.curPid,
					child: this.curCheck
				}
				console.log(data);
				await psMethod.authenticatorPostSync(host, token, data)
					.then(res => {
						console.log('提交物流单', res);
						uni.showToast({
							title: res.resmsg
						})
						this.$emit('eventAccept')
					}).catch(err => {
						console.log('失败', err);
						uni.showToast({
							title: '失败'
						})
					})
			},
			changeCarry(e) {
				console.log('changeCarry', e);
			},
			changePids(e) {
				console.log('changePids', e);
				let tmpData = []
				this.prot.forEach(item => {
					console.log(item);
					if (item.ext_product.pid === e) {
						tmpData.push(item)
					}
				})
				this.allProduct = tmpData
			},
			changeNum(id, e) {
				console.log('changeNum', id, e);
			},
			changeCheck(e) {
				console.log('changeCheck', e.detail.value);
				this.curCheck = e.detail.value
			}
		}
	}
</script>

<style lang="scss" scoped></style>